class LifeBar extends eui.Component{
	protected lpIcons:Heart[]
	protected layer:eui.Group
	private _lp:number
	private _pn:number
	/**指示回合所属的矩形效果 */
	private rectTurn:egret.Shape

	private readonly p1color = 0x1874CD 
	private readonly p2color = 0xB22222
	public constructor() {
		super()
		this.skinName = 'resource/eui_skins/scene/lifepoint.exml'
		this.lpIcons = []
	}

	public set playerNum(v:1|2){
		this._pn = v
		this.drawRect()
	}
	
	public set LP(v:number){
		const t = this
		t._lp = v
		if(t.lpIcons.length < t._lp){
			while(t.lpIcons.length < t._lp){
				const h = new Heart()
				t.lpIcons.push(h)
				t.layer.addChild(h)
			}
			t.layer.validateNow()
			return
		}else if(t.lpIcons.length > t._lp){
			const hs = t.lpIcons.splice(t._lp)
			for(let h of hs){
				h.dispose()
			}
		}
	}


	public async turnIn(){
		const t = this
		const oriY = t.rectTurn.y
		let twY:number
		if(t._pn == 1){
			twY = oriY - 200
		}else{
			twY = oriY + 200
		}
		return new Promise((resolve, reject)=>{
			egret.Tween.get(t.rectTurn)
			.set({
				visible:true,
				alpha:0,
				y:twY
			})
			.to({
				alpha:1,
				y:oriY
			}, 150)
			.call(resolve)
		})	
	}

	public async turnOut(){
		const t = this
		const oriY = t.rectTurn.y
		let twY:number
		if(t._pn == 1){
			twY = oriY - 200
		}else{
			twY = oriY + 200
		}
		return new Promise((resolve, reject)=>{
			egret.Tween.get(t.rectTurn)
			.to({
				alpha:0,
				y:twY
			}, 150)
			.set({
				visible:false,
				y:oriY,
				alpha:1
			})
			.call(resolve)
		})	
	}

	private drawRect(){
		const t = this
		
		const color = t._pn == 1?t.p1color : t.p2color
		const shp = new egret.Shape()
		let mt = new egret.Matrix(1,0,0,1)
		mt.createGradientBox(t.width, t.parent.height)

		shp.graphics.beginGradientFill(egret.GradientType.RADIAL, [color, color], [1, 0], [100, 255], mt)
		// shp.graphics.beginFill(0x000000)
		shp.graphics.drawRect(0, 0, t.width, t.parent.height)
		shp.graphics.endFill()
		t.addChildAt(shp, 0)
		shp.visible = false
		t.rectTurn = shp
	}

}

class Heart extends eui.Component implements IDisposable{
	public constructor(){
		super()
		this.skinName = 'resource/eui_skins/scene/heart.exml'
	}
	
	public dispose(){
		const t = this
		egret.Tween
		.get(t)
		.to({
			scaleX:0,
			scaleY:0
		}, 200)
		.call(()=>[
			t.parent && t.parent.removeChild(t)
		])
		
	}
}